﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace BookWriter
{
    public partial class SearchDialog : Form
    {
        private Form1 mainForm;
        private TreeNode rootNode;
        private TreeNodeCollection rootNodes;
        private int findLength;

        public void Show(Form1 form,TreeNode node)
        {
            mainForm = form;
            rootNode = node;
            rootNodes = null;
            Left = mainForm.Left + (mainForm.Width - Width) / 2;
            Top = mainForm.Top + (mainForm.Height - Height) / 2;
            Show();
        }

        public void Show(Form1 form, TreeNodeCollection nodes)
        {
            mainForm = form;
            rootNode = null;
            rootNodes = nodes;
            Left = mainForm.Left + (mainForm.Width - Width) / 2;
            Top = mainForm.Top + (mainForm.Height - Height) / 2;
            Show();
        }

        public SearchDialog()
        {
            InitializeComponent();
        }

        private void findFile(TreeNodeCollection nodes,List<FileDesc> list){
            foreach(TreeNode node in nodes)
            {
                FileDesc fd = node.Tag as FileDesc;
                if (fd.type == FileType.folder)
                    findFile(node.Nodes, list);
                else
                    list.Add(fd);
            }
        }

        private void tb_find_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                b_find_Click(sender, e);
            }
        }

        private void b_find_Click(object sender, EventArgs e)
        {
            if(tb_find.Text.Length==0) return;

            findLength = tb_find.Text.Length;
            RichTextBoxFinds findOption=RichTextBoxFinds.NoHighlight;
            if(cb_MatchCase.Checked) findOption|=RichTextBoxFinds.MatchCase;
            if(cb_WholeWord.Checked) findOption|=RichTextBoxFinds.WholeWord;
 
            //查找所有被查文件
            List<FileDesc> list = new List<FileDesc>();
            if (rootNode != null)
            {
                FileDesc fd = rootNode.Tag as FileDesc;
                if (fd.type == FileType.folder)
                    findFile(rootNode.Nodes, list);
                else
                    list.Add(fd);  //查找单文件
            }
            else if (rootNodes != null)
                findFile(rootNodes, list);
            
            listView1.Items.Clear();
            //listView1.BeginUpdate();
            RichTextBox rtb = null;
            foreach(FileDesc fd in list)
            {
                RichTextBox tb;
                if(fd.textBox!=null)  //文件已打开
                {
                    tb=fd.textBox.richTextBox;
                }
                else //未打开
                {
                    if (rtb == null) rtb = new RichTextBox();
                    DocumentEditor.LoadFile(fd, rtb);
                    tb=rtb;
                }
                int start = 0;
                while (start >= 0)
                {

                    start = tb.Find(tb_find.Text, start, findOption);
                    if(start>=0)
                    {
                        ListViewItem item = listView1.Items.Add(fd.FullName);
                        item.Tag = fd;
                        item.SubItems.Add(start.ToString());
                        start += tb_find.Text.Length;
                    }
                }
            }
            //listView1.EndUpdate();
            if(rtb!=null)
                rtb.Clear();
            l_msg.Text = "查找到" + listView1.Items.Count + "处";
        }

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                ListViewItem item = listView1.SelectedItems[0];
                FileDesc fd = item.Tag as FileDesc;
                if(fd.page==null)
                {
                    mainForm.addContentPage(fd);
                }
                if(fd.textBox!=null)
                {
                    int start=int.Parse(item.SubItems[1].Text);
                    fd.textBox.richTextBox.Select(start, findLength);
                }
            }

        }

        public void Clear()
        {
            listView1.Items.Clear();
        }

        private void SearchDialog_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            Hide();
        }

        private void b_replace_Click(object sender, EventArgs e)
        {
            if (tb_find.Text.Length == 0 || tb_replace.Text.Length==0) return;

            findLength = tb_replace.Text.Length;
            RichTextBoxFinds findOption = RichTextBoxFinds.NoHighlight;
            if (cb_MatchCase.Checked) findOption |= RichTextBoxFinds.MatchCase;
            if (cb_WholeWord.Checked) findOption |= RichTextBoxFinds.WholeWord;

            //查找所有被查文件
            List<FileDesc> list = new List<FileDesc>();
            if (rootNode != null)
            {
                FileDesc fd = rootNode.Tag as FileDesc;
                if (fd.type == FileType.folder)
                    findFile(rootNode.Nodes, list);
                else
                    list.Add(fd);  //查找单文件
            }
            else if (rootNodes != null)
                findFile(rootNodes, list);

            listView1.Items.Clear();
            //listView1.BeginUpdate();
            RichTextBox rtb = null;
            int file_count = 0, total_count = 0;
            foreach (FileDesc fd in list)
            {
                RichTextBox tb;
                int count = 0;
                if (fd.textBox != null)  //文件已打开
                {
                    tb = fd.textBox.richTextBox;
                }
                else //未打开
                {
                    if (rtb == null) rtb = new RichTextBox();
                    DocumentEditor.LoadFile(fd, rtb);
                    tb = rtb;
                }
                int start = 0;
                while (start >= 0)
                {
                    start = tb.Find(tb_find.Text, start, findOption);
                    if (start >= 0)
                    {
                        ListViewItem item = listView1.Items.Add(fd.FullName);
                        item.Tag = fd;
                        item.SubItems.Add(start.ToString());
                        tb.Select(start, tb_find.Text.Length);
                        tb.SelectedText = tb_replace.Text;  //替换
                        start += tb_replace.Text.Length;
                        count++;
                    }
                }
                if (count > 0) //已经打开的,标记已修改
                {
                    if (tb == rtb)
                        DocumentEditor.SaveFile(fd, tb);
                    file_count++;
                    total_count += count;
                }
            }
            //listView1.EndUpdate();
            if (rtb != null)
                rtb.Clear();
            l_msg.Text = "替换" + file_count + "处，涉及" + total_count + "个文件";
        }


    }
}
